<?php
/**
 * 系统消息
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/7
 * Time: 15:12
 */

namespace app\admin\controller;

use app\admin\business\MessageBus;
use think\facade\Request;
use think\facade\View;
class Message extends Common
{
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 消息列表
     * @return array|string
     * @throws \Exception
     */
    public function index(){
        if (Request::isPost()) {
            $key = input('post.key');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $sort_by = input('sort_by') ? input('sort_by') : 'id';
            $sort_order = input('sort_order') ? input('sort_order') : 'desc';
            $where = [];
            if ($key) {
                $where[] = ['b.title', 'like', "%" . $key . "%"];
            }
            $list = (new MessageBus())->getList($where,$sort_by,$sort_order,$pageSize,$page);
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return View::fetch();
    }


    /**
     * 新增系统消息
     * @return array|string
     */
    public function add()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $data['create_time'] = time();
                $res = (new MessageBus())->add($data);
                if ($res){
                    return ['code' => 0, 'msg' => '新增成功!'];
                }else{
                    return ['code' => 3000, 'msg' => '新增失败'];
                }
            } else {
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 反馈详情
     * @return array|string
     * @throws \Exception
     */
    public function edit(){
        if (Request::isPost()){
            $post = input('post.');
            $where = ['id'=>$post['id']];
            $data = [
                'title' => $post['title'],
                'content' => $post['content']
            ];
            $res = (new MessageBus())->edit($where,$data);
            if ($res){
                return ['code' => 0, 'msg' => '操作成功!'];
            }else{
                return ['code' => 3000, 'msg' => '操作失败'];
            }
        }else{
            $id = input('id', 0, 'intval');
            if (!$id) {
                return ['code' => 1, 'msg' => '非法请求!'];
            }
            $info  = (new MessageBus())->getInfo($id);
            $this->assign('info', $info);
            return View::fetch();
        }
    }


    /**
     * 修改状态
     * @return Array|array
     */
    public function doStatus(){
        try {
            $id = input('request.id');
            $status = input('request.status');
            if (!$id) {
                return ['code' => 3000, 'msg' => '操作失败!'];
            }
            $result = (new MessageBus())->doStatus($id,$status);
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 删除消息
     * @return Array|array
     */
    public function doDelete(){
        try {
            $id = input('request.id');
            if (!$id) {
                return ['code' => 3000, 'msg' => '请选择要删除的数据!'];
            }
            $result = (new MessageBus())->del($id);
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

}